package com.lzx.iteam.net;

import android.os.Message;
import com.lzx.iteam.bean.aidl.ChatMoreMsg;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Megan on 15/7/14.
 */
public class GetQnTokenMsg extends CcMsgStructure {

    private Message mCallBack;
    private byte[] file;
    private String  path;
    private String voice;
    private String fileName;
    private ChatMoreMsg chatMsg;

    public GetQnTokenMsg(Message callback,byte[] file,String fileName) {
        this.mCallBack = callback;
        this.file=file;
        this.fileName=fileName;
    }

    public GetQnTokenMsg(Message callback) {
        this.mCallBack = callback;
    }

    public GetQnTokenMsg(Message callback,byte[] file,String fileName,ChatMoreMsg chatMsg) {
        this.mCallBack = callback;
        this.file=file;
        this.fileName=fileName;
        this.chatMsg=chatMsg;
    }

    public GetQnTokenMsg(Message callback,String path,String fileName,ChatMoreMsg chatMsg) {
        this.mCallBack = callback;
        this.path=path;
        this.fileName=fileName;
        this.chatMsg=chatMsg;
    }


    public void setParams(String path,String fileName,String voice){
        this.path=path;
        this.fileName=fileName;
        this.voice=voice;
    }



    @Override
    public void onReceive(JSONObject data) {
        Map<String ,Object > map=new HashMap<String, Object>();
        try {
            JSONObject item = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            if(item.has("token")){
                map.put("token",item.getString("token"));
                map.put("file",file);
                map.put("fileName",fileName);
                map.put("path",path);
                if(chatMsg!=null){
                    map.put("chatMsg",chatMsg);
                }
            }else if (item.has("tokens")){
                JSONObject token = item.getJSONObject("tokens");
                Iterator keys = token.keys();
                while(keys.hasNext()){
                    String key=(String)keys.next();
                    map.put(key,token.get(key));
                }
                map.put("fileName",fileName);
                map.put("path",path);
                map.put("voice",voice);
            }else{
                Iterator keys = item.keys();
                while(keys.hasNext()){
                    String key=(String)keys.next();
                    map.put(key,item.get(key));
                }
                map.put("fileName",fileName);
                map.put("path",path);
                map.put("voice",voice);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        if (mCallBack != null) {
            mCallBack.arg1 = 0;
            mCallBack.obj=map;
            mCallBack.sendToTarget();
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mCallBack != null) {
            mCallBack.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mCallBack.obj = errorMsg;
            } else {
                mCallBack.obj = Constants.NETWORK_WRROR;
            }
            mCallBack.sendToTarget();
        }
    }
}
